const express = require('express');
const router = express.Router();
const Comment = require('../../model/comment.js');
const User = require('../../model/user')
    //请求square
router.get('/', (req, res, next) => {
    console.log(req.query.cid)
    if (req.query.cid) {
        let cid = req.query.cid
        Comment.findOne({ _id: cid }).populate('user').then(data => {
            let comment = {
                cid: data._id,
                uid: data.user._id,
                nickname: data.user.nickname.toString(),
                avatar: data.user.avatar,
                message: data.message,
                color: data.color,
                img: data.img,
                browse: data.browse,
                great: data.great,
                coms: data.comment
            }
            let count = (data.browse + 1) * 1;
            Comment.findOneAndUpdate({ _id: cid }, { browse: count }, (err, ret) => {
                if (err) {
                    console.log(err)
                }
                return console.log('浏览加1')
            })
            res.send(comment)
        })
    } else {
        Comment.find().populate('user').then((data) => {
            // console.log(data)
            if (data != []) {
                let comments = []
                data.forEach(e => {
                    // console.log(e)
                    let comment = {
                        cid: e._id,
                        uid: e.user._id,
                        nickname: e.user.nickname.toString(),
                        avatar: e.user.avatar,
                        message: e.message,
                        color: e.color,
                        img: e.img,
                        great: e.great,
                        coms: e.comment,
                        browse: e.browse
                    }
                    comments.push(comment)
                });
                // console.log(comments)
                res.send(comments)
            } else {
                res.end()
            }
        }).catch((error) => {
            console.error(error)
        });
    }

    // res.send('已访问到评论页')
});
//接收前端发表的话题信息
router.post('/', (req, res, next) => {
    let data = req.body
    console.log(data._id);
    // mongoose.connect('mongodb://localhost:27018/vue_project', { useNewUrlParser: true, useUnifiedTopology: true }, function(err) {
    //     if (err) {
    //         console.log(err)
    //     }
    //     return
    // });
    if (!data._id) {
        console.log('话题不合法')
        res.send('话题不合法，已清除')
    } else {
        let comment = new Comment({
            user: data._id,
            message: data.content,
            color: data.color,
            img: data.img,
        });
        comment.save()
        res.send('接收到话题信息post')
    }

});
//处理点赞
router.post('/clickgood', (req, res, next) => {
    let cid = req.body.cid.trim();
    let uid = req.body.uid;
    Comment.findOne({ _id: cid }).then(result => {
        let arr = result.great
        console.log(arr)
        let d = arr.indexOf(uid)
        console.log(d)
        if (d == -1) {
            arr.push(uid)
            console.log('用户点赞', arr)
        } else {
            arr.remove(uid)
            console.log('用户取消点赞', arr)
        }
        Comment.findOneAndUpdate({ _id: cid }, { great: arr }, (err, docs) => {
            if (err) {
                return console.log('更新错误')
            }
            console.log(docs)
            res.send('1')
        })
    })
});
//评论处理
router.post('/talk', (req, res, next) => {
    console.log(req.body)
    let cid = req.body.cid;
    let comment = {
        com: req.body.com,
        user: req.body.user
    }
    Comment.findOneAndUpdate({ _id: cid }, {
        $push: {
            comment: comment
        }
    }).then((err, ret) => {
        if (err) {
            console.log(err)
        }
        console.log(ret)
        res.send('1')
    })
});
//根据用户id查询动态
router.get('/findbyid', (req, res, next) => {
    console.log(req.query.uid)
    let uid = req.query.uid
    Comment.find({
        user: uid
    }).then(result => {
        // if (result == []) {
        //     User.findById(uid).then(userInfo => {
        //         let user = {
        //             avatar: userInfo.avatar,
        //             nickname: userInfo.nickname,
        //             uid: userInfo._id,
        //         }
        //         res.send(user)
        //     })
        // }
        // let data = []
        // result.forEach(e => {
        //     let item = {
        //         img: e.img,
        //         browse: e.browse,
        //         comment: e.comment,
        //         great: e.great,
        //         cid: e._id,
        //         nickname: e.user.nickname,
        //         avatar: e.user.avatar,
        //         message: e.message,
        //     }
        //     data.push(item)
        // })
        res.send(result)
    })

})
module.exports = router;